//
//
// adventure screen handler for the Medicine Wagon Object
//
// L. Dean Gibson II  2/13/01
//

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_medicine_wagon.h"

#include "adv_object_model.h"
#include "adventure_ai.h"
#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_sounds.h"
#include "ai_value.h"
#include "army.h"
#include "artifact.h"
#include "artifact_properties.h"
#include "basic_dialog.h"
#include "bitmap_layer_cache_window.h"
#include "bitmap_layer_cache_window_ptr.h"
#include "default_artifacts.h"
#include "external_string.h"
#include "format_string.h"
#include "hero.h"
#include "hero_class_properties.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "random.h"
#include "replace_keywords.h"
#include "simple_dialog.h"
#include "town_type.h"

static t_object_registration<t_adv_medicine_wagon> 
       k_registration( k_adv_object_medicine_wagon );

// -------------------------------------------------------
// Medicine Wagon object constructor
// -------------------------------------------------------
t_adv_medicine_wagon::t_adv_medicine_wagon(  std::string const& source )
			  : t_stationary_adventure_object( source )
{
}

// -------------------------------------------------------
// activate trigger for the Medicine Wagon adventure map object
// -------------------------------------------------------
extern t_external_string const k_text_and;

void t_adv_medicine_wagon::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                               t_direction direction, t_adventure_frame* frame )
{
	t_player* player = army->get_owner();
	if (player == NULL)
		return;

	bool is_human = !player->is_computer();

	std::vector<t_artifact> new_artifact;
	// give the decided artifact to the hero in the army, if any
	for ( int i = 0; i < (int)m_artifact.size(); i++) 
	{
		if (is_human)
		{
			new_artifact.push_back( get_default_artifact(m_artifact[i]) );
			army->add(new_artifact[i]);
		}
		else
			ai_give_artifact_to_army( army->get_creatures(), get_default_artifact(m_artifact[i]) );
	}
	destroy(); // remove us from the map

	// display some sort of response 
	if (!player->is_computer())
	{
		t_basic_dialog*					dialog;
		std::string						text;
		std::string						potion_text;
		t_bitmap_layer_cache_window_ptr icon_window;
		t_bitmap_layer_ptr				icon;

		dialog = new t_basic_dialog;
		text = get_text( "Initial" );

		// add the artifacts name to the displayed text
		for (int i = 0; i < (int)new_artifact.size(); i++)
		{
			potion_text += new_artifact[i].get_name();
			if (i + 2 < (int)new_artifact.size())
				potion_text += ", ";
			else if (i + 1 < (int)new_artifact.size())
				potion_text += " " + k_text_and.get() + " ";
		}

		dialog->set_text( replace_keywords(text, "%artifact_list", potion_text ) );
		
		// add the icon to the window as well...
		for ( int i = 0; i < (int)new_artifact.size(); i++) 
		{
			icon = get_icon( m_artifact[i] );
			icon_window = new t_bitmap_layer_cache_window( icon, t_screen_point(0,0), dialog, false );
			icon_window->set_help_balloon_text( new_artifact[i].get_name() );
			icon_window->set_right_click_text( new_artifact[i].get_help_text() );
			dialog->add_display_window( icon_window, new_artifact[i].get_name() );
		}

		dialog->add_ok_button();
		dialog->set_title( get_name() );
		dialog->open( get_dialog_bottom( point ), true, k_align_bottom );

		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_treasure )->play( get_sound_volume() );
		dialog->run_modal();
		play_music( music );
	}
}

// -------------------------------------------------------
// Medicine Wagon placement function, decides the treasure contained in the backpack at map load time
// -------------------------------------------------------

void t_adv_medicine_wagon::initialize( t_adventure_map& map )
{
	//decide randomly how many potions and then which potions the medicine wagon will give
	int count = random( 2, 3 );
	int i;

	for (i = 0; i < count; i++)
		m_artifact.push_back( map.get_random_potion() );
}


// -------------------------------------------------------
// Medicine Wagon as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_medicine_wagon::write( std::streambuf& stream ) const
{
	int i;

	put<t_uint8>( stream, m_artifact.size() );
	for (i = 0; i < (int)m_artifact.size(); i++)
		put<t_artifact_type>( stream, m_artifact[i] );
	return true;
}

// -------------------------------------------------------
// Medicine Wagon as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_medicine_wagon::read( std::streambuf&						stream, 
						         t_qualified_adv_object_type const&	type,
						         int									version )
{
	int count;
	int i;

	count = get<t_uint8>( stream );
	for (i = 0; i < count; i++)
		m_artifact.push_back( get<t_artifact_type>( stream ));
	return true;
}

// -------------------------------------------------------
// Medicine Wagon as it appears on the adventure map
// -------------------------------------------------------
float t_adv_medicine_wagon::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	float value = army.get_owner()->ai_get_artifact_value( k_artifact_level_item ) * 2;
	
	return std::max(value, k_ai_minimum_artifact_value_level);
}

